/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entitas.User;
import entitas.DaftarUser;
import java.util.ArrayList;
import java.util.List;
import jpa.exceptions.NonexistentEntityException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Rido
 */
public class LogicUser {

    HttpServletRequest request;

    public LogicUser(HttpServletRequest request) {
        this.request = request;
    }

    /* method untuk melakukan fungsi input user, yaitu mengambil nilai textfield pada form user
     * untuk kemudian dimasukkan sebagai nilai untuk membuat baris baru pada tabel user
     */
    public String inputUser() {
        User user = new User();
        DaftarUser du = new DaftarUser();
        HttpSession session = request.getSession();

        String username = request.getParameter("username");
        String namaLengkap = request.getParameter("namalengkap");
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        user.setUsername(username);
        user.setNamaLengkap(namaLengkap);
        user.setEmail(email);
        user.setPassword(password);

        if (username.equals("") || namaLengkap.equals("") || email.equals("")
                || password.equals("")) {
            return "errorUserKosong.jsp";
        } else {
            boolean hasilCheck = du.checkUser(username);
            if (!hasilCheck) {
                du.createUser(user);
                return "loginForm.jsp";
            } else {
                return "errorUsernameSama.jsp";
            }
        }
    }

    public String lihatUser() {
        List<User> user = new ArrayList<User>();
        DaftarUser du = new DaftarUser();

        user = du.getUsers();
        request.setAttribute("users", user);
        return "daftarUser.jsp";

    }

    /* method untuk melakukan fungsi login yaitu mengecek apakah username dan password 
     * yang dimasukkan ke dalam form login sudah sesuai
     */
    public String loggedIn() {
        HttpSession session = request.getSession();
        User user = new User();
        DaftarUser du = new DaftarUser();
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        if (username.equals("") || password.equals("")) {
            //session.setAttribute("error", "masih ada field kosong");
            return "errorLoginKosong.jsp";
        } else if (username.equalsIgnoreCase("admin") && password.equalsIgnoreCase("admin")) {
            return "index.jsp";
        } else {
            boolean hasil = du.checkUser(username);
            if (hasil) {
                user = null;
                user = du.getUser(username);
                if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
                    session.setAttribute("username", user);
                    return "userHome.jsp";
                } else {
                    return "errorPassword.jsp";
                }
            } else {
                return "errorUsername.jsp";
            }
        }
    }

    /* method untuk logout yaitu dengan cara
     * menghapus session sehingga session kembali menjadi null
     */
    public String logout() {
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            session.removeAttribute("username");
            session.invalidate();
            return "loginForm.jsp";
        } else {
            session.setAttribute("error", "Anda sudah logout sebelumnya");
            return "loginForm.jsp";
        }
    }

    public String ubahProfil() {
        HttpSession session = request.getSession();
        User user = new User();
        List<User> users = new ArrayList<User>();
        DaftarUser du = new DaftarUser();

        if (session.getAttribute("username") !=null){
            User username = (User) session.getAttribute("username");
            boolean hasilCheck = du.checkUser(username.getUsername());
            if (hasilCheck) {
                user = username;
                session.setAttribute("tempEdit", user);
                return "editProfil.jsp";
            } else{
                session.setAttribute("error", "username tidak ada");
                users = du.getUsers();
                request.setAttribute("users", users);
                session.setAttribute("show", "<a href='servletuser?show=userhome&&show=all'> show All user </a>");
                return "userHome.jsp";
            }
        } else{
            session.setAttribute("error", "sudah Logout sebelumnya");
            users = du.getUsers();
            request.setAttribute("users", users);
            session.setAttribute("show", "<a href='servletuser?show=userhome&&show=all'> show All user </a>");
            return "userHome.jsp";
        }
    }

     public String SimpanUbahProfil() {
        HttpSession session = request.getSession();
        User user = new User();
        DaftarUser du = new DaftarUser();
        List<User> users = new ArrayList<User>();
        
        String username = request.getParameter("username");
        String namaLengkap = request.getParameter("namalengkap");
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        if(session.getAttribute("username") !=null){
            user =(User) session.getAttribute("username");
            String tempUsername = user.getUsername();
            if (username !=null){
            user.setUsername(username);
            user.setNamaLengkap(namaLengkap);
            user.setEmail(email);
            user.setPassword(password);
            session.setAttribute("tempEdit", user);
            }else {
                session.setAttribute("error", "masih ada field kosong");
                return "editProfil.jsp";
            }

            if (username.equals("") || namaLengkap.equals("") || email.equals("") || password.equals("")) {
                session.setAttribute("error", "masih ada field kosong");
                return "editProfil.jsp";
            } else {
                    if (password.length() > 5) {
                        boolean hasilCheck = du.checkUser(username);
                        if (!hasilCheck || username.equals(tempUsername)) {
                            session.setAttribute("uname", user);
                            du.editUser(user);
                            session.removeAttribute("tempEdit");
                        } else {
                            session.setAttribute("error", "username sudah terpakai");
                            return "editProfil.jsp";
                        }
                    } else {
                        session.setAttribute("error", "password minimal 6");
                        return "editProfil.jsp";
                    }
                } 
            }else {
            session.setAttribute("error", "sudah logout sebelumnya");
            users = du.getUsers();
            request.setAttribute("users", users);
            session.setAttribute("show", "<a href='servletuser?show=home&&show=all'>Show All Obats</a>");
            return "userHome.jsp";
        }return "userHome.jsp";
    }

     public String hapusUser() throws NonexistentEntityException{
        HttpSession session = request.getSession();
        String idUser = request.getParameter("idUser");
        List<User> user = new ArrayList<User>();
        DaftarUser du = new DaftarUser();

        if (session.getAttribute("username") != null) {
            if (idUser == null) {
                user = du.getUsers();
                request.setAttribute("username", user);
            } else {
                int id = Integer.parseInt(idUser);
               du.deleteUser(id);
                user = du.getUsers();
                request.setAttribute("username", user);
            }
            return "daftarUser.jsp";
        } else {
            return "index.jsp";
        }
    }
}

